package Interfaz;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JSplitPane;

import java.awt.BorderLayout;

import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import Lexico.textFile;
import Main.MyCompilator;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Dimension;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

import java.awt.Component;
import java.io.File;

public class VentanaPrincipal {
	
	private MyCompilator compilador;
	private String ruta;
	private JFrame frmMycompilator;	
	
	//Cosas de interfaz
	private JTextPane textLineas = new JTextPane();
	private JTextPane textArchivo = new JTextPane();
	private JTextPane textLexico = new JTextPane();
	private JTextPane textGeneral = new JTextPane();
	private JTextPane textTablaSimbolos = new JTextPane();
	private JTextPane textSintactico = new JTextPane();
	private JTextPane textTercetos = new JTextPane();
	private JTextPane textCodigoIntermedio = new JTextPane();
	private JMenuItem botonCompilar = new JMenuItem("Compilar");
	private JMenuItem botonGuardar = new JMenuItem("Guardar");

	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					VentanaPrincipal window = new VentanaPrincipal();
					window.frmMycompilator.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public VentanaPrincipal() {
		initialize();
	}
	
	private void initialize() {
		frmMycompilator = new JFrame();
		frmMycompilator.setMinimumSize(new Dimension(600, 800));
		frmMycompilator.setTitle("MyCompilator");
		frmMycompilator.setBounds(100, 100, 450, 300);
		frmMycompilator.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		JMenuBar menuBar = new JMenuBar();
		frmMycompilator.setJMenuBar(menuBar);
		
		JMenuItem mntmAbrir = new JMenuItem("Abrir");
		mntmAbrir.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser = new JFileChooser();
			    FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivos de texto", "txt");
			    chooser.setFileFilter(filter);
			    chooser.setCurrentDirectory(new File("C:\\"));
			    int returnVal = chooser.showOpenDialog(frmMycompilator);
			    if(returnVal == JFileChooser.APPROVE_OPTION) {
			    	ruta= chooser.getSelectedFile().getPath();
			    	botonGuardar.setEnabled(true);
			    	botonCompilar.setEnabled(true);
			    	muestraArchivo();
			    	limpiaConsola();
			    }			
			}
		});
		menuBar.add(mntmAbrir);
		botonCompilar.setEnabled(false);
		
		
		botonCompilar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				//GuardarArchivo();
				muestraArchivo();
				compilador= new MyCompilator(ruta);
				compilador.ejecutar();
				verTodo();
			}
		});
		menuBar.add(botonCompilar);
		
		
		botonGuardar.setEnabled(false);
		botonGuardar.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				GuardarArchivo();
			}
		});
		menuBar.add(botonGuardar);
		
		JMenuItem botonGuardarComo = new JMenuItem("Guardar Como");
		botonGuardarComo.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				JFileChooser chooser = new JFileChooser();
			    FileNameExtensionFilter filter = new FileNameExtensionFilter("Archivos de texto", "txt");
			    chooser.setFileFilter(filter);
			    chooser.setCurrentDirectory(new File("C:\\"));
			    int returnVal = chooser.showOpenDialog(frmMycompilator);
			    if(returnVal == JFileChooser.APPROVE_OPTION) {
			    	String aa= chooser.getSelectedFile().getPath();
			    	ruta= aa;
			    	botonGuardar.setEnabled(true);
			    	botonCompilar.setEnabled(true);
			    	GuardarArchivo();
			    }				
			}
		});
		menuBar.add(botonGuardarComo);
		
		JSplitPane splitPane = new JSplitPane();
		splitPane.setOneTouchExpandable(true);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		frmMycompilator.getContentPane().add(splitPane, BorderLayout.CENTER);
		
		JSplitPane splitPane_1 = new JSplitPane();
		splitPane_1.setMinimumSize(new Dimension(217, 600));
		splitPane_1.setEnabled(false);
		splitPane_1.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setLeftComponent(splitPane_1);		
		JLabel lblCodigo = new JLabel("Codigo: ");
		lblCodigo.setEnabled(false);
		splitPane_1.setLeftComponent(lblCodigo);		
		JScrollPane scrollPane = new JScrollPane();
		splitPane_1.setRightComponent(scrollPane);		
		JSplitPane splitPane_2 = new JSplitPane();
		scrollPane.setViewportView(splitPane_2);
		textLineas.setMinimumSize(new Dimension(30, 6));
		textLineas.setEditable(false);
		textLineas.setEnabled(false);		
		splitPane_2.setLeftComponent(this.textLineas);		
		splitPane_2.setRightComponent(this.textArchivo);		
		JSplitPane splitPane_3 = new JSplitPane();
		splitPane_3.setEnabled(false);
		splitPane_3.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setRightComponent(splitPane_3);		
		JLabel lblConsola = new JLabel("Consola:");
		lblConsola.setEnabled(false);
		splitPane_3.setLeftComponent(lblConsola);		
		JScrollPane scrollPane_1 = new JScrollPane();
		splitPane_3.setRightComponent(scrollPane_1);		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		scrollPane_1.setViewportView(tabbedPane);
		JPanel panel_5 = new JPanel();
		tabbedPane.addTab("General", null, panel_5, null);		
		textGeneral.setMinimumSize(new Dimension(550, 200));
		textGeneral.setEditable(false);
		textGeneral.setAlignmentY(Component.TOP_ALIGNMENT);
		textGeneral.setAlignmentX(Component.LEFT_ALIGNMENT);
		panel_5.add(textGeneral);		
		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Lexico", null, panel_1, null);
		textLexico.setEditable(false);		
		panel_1.add(this.textLexico);		
		JPanel panel_4 = new JPanel();
		tabbedPane.addTab("Sintactico", null, panel_4, null);		
		textSintactico.setEditable(false);
		panel_4.add(textSintactico);				
		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("Assembler", null, panel_3, null);		
		panel_3.add(textCodigoIntermedio);
		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("Tabla Simbolos", null, panel_2, null);		
		textTablaSimbolos.setEditable(false);
		panel_2.add(textTablaSimbolos);		
		JPanel panel = new JPanel();
		tabbedPane.addTab("Tercetos", null, panel, null);		
		panel.add(textTercetos);
	}
	
	private void verTodo(){		
		this.textGeneral.setText(this.compilador.verGeneral());
		this.textLexico.setText(this.compilador.verLexico());
		this.textTablaSimbolos.setText(this.compilador.verTablaSimbolos());
		this.textSintactico.setText(this.compilador.verSintactico());	
		this.textTercetos.setText(this.compilador.verTercetos());
		this.textCodigoIntermedio.setText(this.compilador.verCodigoIntermedio());
	}
	
	private void limpiaConsola(){	
		this.textGeneral.setText("");
		this.textLexico.setText("");
		this.textTablaSimbolos.setText("");
		this.textSintactico.setText("");	
	}
	
	private void muestraArchivo(){
		textFile arch= new textFile(this.ruta);
		String lineas="";
		String aux= arch.getLineaActual();
		String codigo="";
		while (aux!=null){
			codigo= codigo + aux + '\n';
			lineas= lineas + (arch.getNumeroLinea()-1) + " - "+ '\n';
			aux= arch.getLineaActual();
		}
		this.textLineas.setText(lineas);
		this.textArchivo.setText(codigo);				
	}
	private void GuardarArchivo(){
		textFile arch= new textFile(this.ruta);
		char[] aa= this.textArchivo.getText().toCharArray();
		arch.guardarArchivo(aa);
		this.muestraArchivo();
	}

}
